/**
 * Grid.as
 * Copyright 2014 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */
package org.bridge.flash.hi2048.view
{
    import flash.display.Sprite;

    import spark.components.TextInput;

    /**
     * @author zhangshuai
     */
    public class Grid extends Sprite
    {
        public static const GRID_SIDE_LENGTH:int = 105;
        public static const GRID_PADDING:int = 16;

        private var mValue:Object = {n: 0};
        private var mLabel:TextInput;

        public function Grid(i:int, j:int, value:Object)
        {
            x = (j + 1) * GRID_PADDING + j * GRID_SIDE_LENGTH;
            y = (i + 1) * GRID_PADDING + i * GRID_SIDE_LENGTH;
            mValue = value;
            init();
            render();
        }

        private function init():void
        {
            mLabel = new TextInput;
            addChild(mLabel);
        }

        private function render():void
        {
            var fillcolor:uint = 0xD2E9FF;
            var detla:uint = 0x0E0800;
            if (mValue.n != 0)
            {
                mLabel.text = mValue.n;
                fillcolor = fillcolor - detla * Math.log(mValue.n) / Math.log(2);
            }
            else
            {
                mLabel.text = null;
            }
            graphics.beginFill(fillcolor);
            graphics.drawRect(0, 0, GRID_SIDE_LENGTH, GRID_SIDE_LENGTH);
            graphics.endFill();
        }

        public function refresh():void
        {
            if (mLabel.text != mValue.n.toString())
            {
                render();
            }
        }
    }
}
